﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassModel;
using System.Data.SqlClient;

namespace ClassDAL
{
    public class t_class_sections_DAL
    {
        /// <summary>
        /// 插入某个fileid -> excel 文件对应的上课节次设置
        /// </summary>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static int InsertSections(t_class_sections sections)
        {
            if (CheckExistWithFileID(sections.taskid)) return -1;

            string insertSQL = "insert into t_class_sections values(@AMSections,@PMSections,@EVSections,@dayofweek,@taskid);select @@identity;";

            SqlParameter[] insertParams = new SqlParameter[] 
            {
                new SqlParameter("@taskid",sections.taskid),
                new SqlParameter("@AMSections",sections.AMSections),
                new SqlParameter("@PMSections",sections.PMSections),
                new SqlParameter("@EVSections",sections.EVSections),
                new SqlParameter("@dayofweek",sections.dayOfWeek)
            };


            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.GetConnection(), System.Data.CommandType.Text, insertSQL, insertParams));
        }

        /// <summary>
        /// 检查当前excel对应的节次设置是否存存在
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public static bool CheckExistWithFileID(int fileid)
        {
            string checkSQL = "select count(1) from t_class_sections where taskid = @taskid;";
            SqlParameter checkParam = new SqlParameter()
            {
                ParameterName = "@taskid",
                Value = fileid
            };


            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.GetConnection(), System.Data.CommandType.Text, checkSQL, checkParam)) > 0;
        }


        /// <summary>
        /// 获取每个excel表格对应的上个节次和一周几节课
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns></returns>
        public static t_class_sections GetSectionByTaskID(int taskid)
        {
            string selectSQL = "select taskid,amsections,pmsections,evsections,dayofweek from t_class_sections where taskid = @taskid; ";

            SqlParameter selectParam = new SqlParameter()
            {
                ParameterName = "@taskid",
                Value = taskid
            };


            SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.GetConnection(), System.Data.CommandType.Text, selectSQL, selectParam);

            if (!reader.Read()) return null;

            t_class_sections section = new t_class_sections();
            section.taskid = Convert.ToInt32(reader["taskid"]);
            section.AMSections= Convert.ToInt32(reader["amsections"]);
            section.PMSections = Convert.ToInt32(reader["pmsections"]);
            section.EVSections = Convert.ToInt32(reader["evsections"]);
            section.dayOfWeek = Convert.ToInt32(reader["dayofweek"]);

            reader.Close();


            return section;
        }


        /// <summary>
        /// 检测当前任务是否设定了排课节次
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns></returns>
        public static bool isSectionIsExistsWithTaskId(int taskid)
        {
            string checkSQL = "select COUNT(1) from t_class_sections where taskid = @taskid;";
            SqlParameter checkParam = new SqlParameter() 
            {
                ParameterName = "@taskid",
                Value = taskid
            };

            var obj = SQLHelper.ExecuteScalar(SQLHelper.GetConnection(), System.Data.CommandType.Text, checkSQL, checkParam);
            return Convert.ToInt32(obj) > 0;
        }
    }
}
